package com.slm.report.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.hjq.bar.OnTitleBarListener;
import com.othershe.nicedialog.BaseNiceDialog;
import com.slm.mylibrary.base.BaseActivity;
import com.slm.mylibrary.utils.DialogUtils;
import com.slm.mylibrary.utils.IDialogAlertCallback;
import com.slm.report.adapter.AddStudentAdapter;
import com.slm.report.databinding.ActivityAddStudentInfoBinding;
import com.slm.report.db.ContentDataBean;
import com.slm.report.db.ContentType;
import com.slm.report.db.StudentInfoBean;
import com.slm.report.utils.PinyinUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName: My Application
 * @Package: com.slm.report.ui
 * @ClassName: AddSudentInfoActivity
 * @Description: java类作用描述
 * @Author: 王丹丹
 * @CreateDate: 2020/6/2 9:11
 * @UpdateUser: 更新者
 * @UpdateDate: 2020/6/2 9:11
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class AddStudentInfoActivity extends BaseActivity {
    private ActivityAddStudentInfoBinding addStudentInfoBinding;
    private List<ContentDataBean> contentDataBeans = new ArrayList<>();
    private String[] baseTitles = new String[]{"姓名", "性别", "年龄", "填表日期", "学校", "年级", "班级", "联系电话", "身高(cm)",
            "会员开始时间", "会员结束时间"};
    private String[] nianJis = new String[]{"幼儿园小班", "幼儿园中班", "幼儿园大班", "一年级", "二年级", "三年级", "四年级",
            "五年级", "六年级", "初一", "初二", "初三", "高一", "高二", "高三"};
    private ContentType[] baseTypes = new ContentType[]{ContentType.SINGLE_LINE_TEXT, ContentType.RADIO
            , ContentType.CHOICE_TV, ContentType.TIME_TV, ContentType.SCHOOL_CHOICE_TV, ContentType.CHOICE_TV, ContentType.SINGLE_LINE_TEXT, ContentType.SINGLE_LINE_TEXT
            , ContentType.SINGLE_LINE_TEXT, ContentType.TIME_TV, ContentType.TIME_TV};

    private String[] visionTitles = new String[]{"裸眼视力", "复查裸眼视力", "带镜史"};
    private ContentType[] visionTypes = new ContentType[]{ContentType.VISION_TEXT, ContentType.VISION_TEXT, ContentType.HIS_VISION_TEXT};

    private String[] qgTitles = new String[]{"球镜", "柱镜", "轴度"};
    private ContentType[] qgTypes = new ContentType[]{ContentType.VISION_TEXT, ContentType.VISION_TEXT
            , ContentType.VISION_TEXT};

    private List<ContentDataBean> baseContentList = new ArrayList<>();
    private List<ContentDataBean> qgContentList = new ArrayList<>();
    private List<ContentDataBean> visionList = new ArrayList<>();
    private StudentInfoBean studentInfoBean;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addStudentInfoBinding = ActivityAddStudentInfoBinding.inflate(LayoutInflater.from(this));
        setContentView(addStudentInfoBinding.getRoot());
        if (getIntent() != null && getIntent().getSerializableExtra("data") != null) {
            studentInfoBean = (StudentInfoBean) getIntent().getSerializableExtra("data");
        }
        if (studentInfoBean == null) {
            addStudentInfoBinding.addView.setText("添   加");
        } else {
            addStudentInfoBinding.addView.setText("变   更");
        }

        //基本信息
        for (int i = 0; i < baseTitles.length; i++) {
            ContentDataBean dataBean = new ContentDataBean();
            dataBean.setGroupTitle("基本信息");
            dataBean.setTitleStr(baseTitles[i]);
            dataBean.setType(baseTypes[i]);
            baseContentList.add(dataBean);
            if ("年龄".equals(baseTitles[i])) {
                List<String> items = new ArrayList<>();
                for (int j = 3; j < 40; j++) {
                    items.add(j + "");
                }
                dataBean.setChoiceItems(items);
            } else if ("年级".equals(baseTitles[i])) {
                List<String> items = new ArrayList<>();
                for (String nianJi : nianJis) {
                    items.add(nianJi);
                }
                dataBean.setChoiceItems(items);
            }
        }
        for (int i = 0; i < visionTitles.length; i++) {
            ContentDataBean dataBean = new ContentDataBean();
            dataBean.setGroupTitle("视力情况");
            dataBean.setTitleStr(visionTitles[i]);
            dataBean.setType(visionTypes[i]);
            visionList.add(dataBean);
        }

        for (int i = 0; i < qgTitles.length; i++) {
            ContentDataBean dataBean = new ContentDataBean();
            dataBean.setGroupTitle("屈光度检查");
            dataBean.setTitleStr(qgTitles[i]);
            dataBean.setType(qgTypes[i]);
            qgContentList.add(dataBean);
        }
        contentDataBeans.addAll(baseContentList);
        contentDataBeans.addAll(visionList);
        contentDataBeans.addAll(qgContentList);


        if (studentInfoBean != null) {
            contentDataBeans.get(0).setContent(studentInfoBean.getName());
            contentDataBeans.get(1).setContent(studentInfoBean.getSex());
            contentDataBeans.get(2).setContent(studentInfoBean.getAge());
            contentDataBeans.get(3).setContent(studentInfoBean.getCreateTime());
            contentDataBeans.get(4).setContent(studentInfoBean.getSchoolName());
            contentDataBeans.get(5).setContent(studentInfoBean.getNianji());
            contentDataBeans.get(6).setContent(studentInfoBean.getBanName());
            contentDataBeans.get(7).setContent(studentInfoBean.getPhone());
            contentDataBeans.get(8).setContent(studentInfoBean.getHeight());
            contentDataBeans.get(9).setContent(studentInfoBean.getVipStartTime());
            contentDataBeans.get(10).setContent(studentInfoBean.getVipEndTime());
            contentDataBeans.get(11).getValueMap().put("left", studentInfoBean.getLuoLeftVal());
            contentDataBeans.get(11).getValueMap().put("right", studentInfoBean.getLuoRightVal());
            contentDataBeans.get(12).getValueMap().put("left", studentInfoBean.getFuLeftVal());
            contentDataBeans.get(12).getValueMap().put("right", studentInfoBean.getFuRightVal());

            contentDataBeans.get(13).getValueMap().put("key", studentInfoBean.getIsHaveHis());
            contentDataBeans.get(13).getValueMap().put("left", studentInfoBean.getHisLeftVal());
            contentDataBeans.get(13).getValueMap().put("right", studentInfoBean.getHisRightVal());


            contentDataBeans.get(14).getValueMap().put("left", studentInfoBean.getQiuLeftVal());
            contentDataBeans.get(14).getValueMap().put("right", studentInfoBean.getQiuRightVal());
            contentDataBeans.get(15).getValueMap().put("left", studentInfoBean.getZhuLeftVal());
            contentDataBeans.get(15).getValueMap().put("right", studentInfoBean.getZhuRightVal());
            contentDataBeans.get(16).getValueMap().put("left", studentInfoBean.getZhouLeftVal());
            contentDataBeans.get(16).getValueMap().put("right", studentInfoBean.getZhouRightVal());

        }

        AddStudentAdapter studentAdapter = new AddStudentAdapter(this, contentDataBeans, true);
        addStudentInfoBinding.list.setAdapter(studentAdapter);
        addStudentInfoBinding.addView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BaseNiceDialog dialog = DialogUtils.obtainCommonDialog("确定提交？", new IDialogAlertCallback() {
                    @Override
                    public void sure() {
                        submitData();
                    }
                });
                dialog.show(getSupportFragmentManager());
            }
        });
        addStudentInfoBinding.titleBar.setOnTitleBarListener(new OnTitleBarListener() {
            @Override
            public void onLeftClick(View v) {
                finish();
            }

            @Override
            public void onTitleClick(View v) {

            }

            @Override
            public void onRightClick(View v) {

            }
        });
    }

    private void submitData() {
        if (StringUtils.isEmpty(baseContentList.get(0).getContent())) {
            ToastUtils.showShort("请输入用户名");
            return;
        }
        if (studentInfoBean == null) {
            studentInfoBean = new StudentInfoBean();
        }

        studentInfoBean.setName(baseContentList.get(0).getContent());
        studentInfoBean.setPyName(PinyinUtils.getPinYin(baseContentList.get(0).getContent()));
        studentInfoBean.setFisrtSpell(PinyinUtils.getFirstSpell(baseContentList.get(0).getContent().substring(0, 1)));

        studentInfoBean.setSex(baseContentList.get(1).getContent());
        studentInfoBean.setAge(baseContentList.get(2).getContent());
        studentInfoBean.setCreateTime(baseContentList.get(3).getContent());
        studentInfoBean.setSchoolName(baseContentList.get(4).getContent());
        studentInfoBean.setNianji(baseContentList.get(5).getContent());
        studentInfoBean.setBanName(baseContentList.get(6).getContent());
        studentInfoBean.setPhone(baseContentList.get(7).getContent());
        studentInfoBean.setHeight(baseContentList.get(8).getContent());
        studentInfoBean.setVipStartTime(baseContentList.get(9).getContent());
        studentInfoBean.setVipEndTime(baseContentList.get(10).getContent());

        studentInfoBean.setQiuLeftVal(qgContentList.get(0).getValueMap().get("left"));
        studentInfoBean.setQiuRightVal(qgContentList.get(0).getValueMap().get("right"));
        studentInfoBean.setZhuLeftVal(qgContentList.get(1).getValueMap().get("left"));
        studentInfoBean.setZhuRightVal(qgContentList.get(1).getValueMap().get("right"));
        studentInfoBean.setZhouLeftVal(qgContentList.get(2).getValueMap().get("left"));
        studentInfoBean.setZhouRightVal(qgContentList.get(2).getValueMap().get("right"));

        studentInfoBean.setLuoLeftVal(visionList.get(0).getValueMap().get("left"));
        studentInfoBean.setLuoRightVal(visionList.get(0).getValueMap().get("right"));
        studentInfoBean.setFuLeftVal(visionList.get(1).getValueMap().get("left"));
        studentInfoBean.setFuRightVal(visionList.get(1).getValueMap().get("right"));
        studentInfoBean.setIsHaveHis(visionList.get(2).getValueMap().get("key"));
        studentInfoBean.setHisRightVal(visionList.get(2).getValueMap().get("right"));
        studentInfoBean.setHisLeftVal(visionList.get(2).getValueMap().get("left"));
        if (studentInfoBean.getId()==0) {
            studentInfoBean.save();
            ToastUtils.showShort("保存用户成功 ");
        } else {
            studentInfoBean.update();
            ToastUtils.showShort("更新用户成功 ");
        }
        Intent intent = new Intent();
        intent.putExtra("data", studentInfoBean);
        setResult(RESULT_OK, intent);
        finish();

    }
}
